package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Refund;
import com.ruoyi.system.service.ILoansService;
import com.ruoyi.system.service.IRefundService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BankSend;
import com.ruoyi.system.service.IBankSendService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 放款Controller
 * 
 * @author ruoyi
 * @date 2021-09-27
 */
@RestController
@RequestMapping("/system/send")
public class BankSendController extends BaseController
{
    @Autowired
    private IBankSendService bankSendService;

    @Autowired
    private ILoansService loansService;

    @Autowired
    private IRefundService refundService;

    /**
     * 查询放款列表
     */
    @PreAuthorize("@ss.hasPermi('system:send:list')")
    @GetMapping("/list")
    public TableDataInfo list(BankSend bankSend)
    {
        startPage();
        List<BankSend> list = bankSendService.selectBankSendList(bankSend);
        return getDataTable(list);
    }

    /**
     * 导出放款列表
     */
    @PreAuthorize("@ss.hasPermi('system:send:export')")
    @Log(title = "放款", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BankSend bankSend)
    {
        List<BankSend> list = bankSendService.selectBankSendList(bankSend);
        ExcelUtil<BankSend> util = new ExcelUtil<BankSend>(BankSend.class);
        return util.exportExcel(list, "放款数据");
    }

    /**
     * 获取放款详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:send:query')")
    @GetMapping(value = "/{senId}")
    public AjaxResult getInfo(@PathVariable("senId") Long senId)
    {
        return AjaxResult.success(bankSendService.selectBankSendBySenId(senId));
    }

    /**
     * 新增放款
     */
    @PreAuthorize("@ss.hasPermi('system:send:add')")
    @Log(title = "放款", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BankSend bankSend)
    {
        return toAjax(bankSendService.insertBankSend(bankSend));
    }

    /**
     * 修改放款
     */
    @PreAuthorize("@ss.hasPermi('system:send:edit')")
    @Log(title = "放款", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BankSend bankSend)
    {
        return toAjax(bankSendService.updateBankSend(bankSend));
    }

    /**
     * 删除放款
     */
    @PreAuthorize("@ss.hasPermi('system:send:remove')")
    @Log(title = "放款", businessType = BusinessType.DELETE)
	@DeleteMapping("/{senIds}")
    public AjaxResult remove(@PathVariable Long[] senIds)
    {
        return toAjax(bankSendService.deleteBankSendBySenIds(senIds));
    }


    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:send:edit')")
    @Log(title = "放款管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeSendStatus")
    public AjaxResult changeStatus(@RequestBody BankSend bankSend)
    {
        Refund refund = new Refund();
        refund.setRefBankId(bankSend.getSenBankId());
        refund.setRefLoId(bankSend.getSenLoaId());
        refund.setRefComNo(String.valueOf(bankSend.getSenComId()));
        refundService.insertRefund(refund);
        loansService.updateLoaSenState(bankSend.getSenLoaId());
        bankSend.setUpdateBy(getUsername());
        return toAjax(bankSendService.updateBankSend(bankSend));
    }
}
